package com.pay.config;

import com.pay.util.EnhanceJedisSentinelPool;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;


import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisSentinelPool;

/**
 * 
 * @author jian.cao
 *
 */
//@Configuration
//@PropertySource("classpath:/application.properties")
public class RedisConfig extends CachingConfigurerSupport {

    @Value("${redis.sentinel.master}")
    private String master;
    @Value("${redis.sentinel.nodes}")
    private String nodes;
    @Value("${redis.password}")
    private String password;
    @Value("${redis.dbIndex}")
    private String dbIndex;
    @Value("${redis.pool.maxIdle}")
    private String maxidle;
    @Value("${redis.pool.minIdle}")
    private String minidle;
    @Value("${redis.pool.maxTotal}")
    private String maxTotal;
    @Value("${redis.connection.timeout}")
    private String timeout;
    
    @Value("${redis.pool.blockWhenExhausted}")
    private String blockWhenExhausted;
    @Value("${redis.pool.maxWaitMillis}")
    private String maxWaitMillis;
    @Value("${redis.pool.testOnBorrow}")
    private String testOnBorrow;
    @Value("${redis.pool.testOnReturn}")
    private String testOnReturn;
    @Value("${redis.pool.testWhileIdle}")
    private String testWhileIdle;
    
    @Value("${redis.pool.minEvictableIdleTimeMillis}")
    private String minEvictableIdleTimeMillis;
    @Value("${redis.pool.softMinEvictableIdleTimeMillis}")
    private String softMinEvictableIdleTimeMillis;
    @Value("${redis.pool.timeBetweenEvictionRunsMillis}")
    private String timeBetweenEvictionRunsMillis;
    
    @Bean
    public JedisPoolConfig jedisPoolConfig() {
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMinIdle(Integer.parseInt(minidle));
        poolConfig.setMaxIdle(Integer.parseInt(maxidle));
        poolConfig.setMaxTotal(Integer.parseInt(maxTotal));
        
        poolConfig.setBlockWhenExhausted( Boolean.parseBoolean(blockWhenExhausted));
        poolConfig.setMaxWaitMillis(Long.valueOf(maxWaitMillis));
        poolConfig.setTestOnBorrow(Boolean.parseBoolean(testOnBorrow));
        poolConfig.setTestOnReturn(Boolean.parseBoolean(testOnReturn));
        poolConfig.setTestWhileIdle(Boolean.parseBoolean(testWhileIdle));
        
        poolConfig.setMinEvictableIdleTimeMillis(Long.valueOf(minEvictableIdleTimeMillis));
        poolConfig.setSoftMinEvictableIdleTimeMillis(Long.valueOf(softMinEvictableIdleTimeMillis));
        poolConfig.setTimeBetweenEvictionRunsMillis(Long.valueOf(timeBetweenEvictionRunsMillis));
        return poolConfig;
    }
    
    @Bean
    public JedisSentinelPool jedisSentinelPool(){
    	JedisSentinelPool jedisSentinelPool = 	new EnhanceJedisSentinelPool(master, nodes, jedisPoolConfig(),
    												Integer.parseInt(timeout),password, Integer.parseInt(dbIndex));
    	return jedisSentinelPool;
    }
    
}
